package com.voicechat.firstmet.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseActivity;
import com.voicechat.firstmet.bean.MySelfInfoBean;
import com.voicechat.firstmet.net.HTTPAPI;
import com.voicechat.firstmet.net.service.MeInfoService;
import com.voicechat.firstmet.utils.ParcelableUtils;
import com.voicechat.firstmet.utils.PhoneUtils;
import com.voicechat.firstmet.utils.SPUtils;
import com.voicechat.firstmet.utils.StatusBarUtils;
import com.voicechat.firstmet.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.activity_imessage
 * @ClassName: AlipayBindingActivity
 * @Description: 支付宝账户（提现）绑定页面
 * @Author: ljp
 * @CreateDate: 2018/10/11 9:25
 * @Version: 1.0
 */
public class AlipayBindingActivity extends BaseActivity {
    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_alipay_account)
    EditText tvAlipayAccount;
    @BindView(R.id.linear_account_safely)
    LinearLayout linearAccountSafely;
    @BindView(R.id.tv_alipay_name)
    EditText tvAlipayName;
    @BindView(R.id.tv_sure)
    TextView tvSure;
    private boolean isName;
    private boolean isAccount;
    private MySelfInfoBean.ObjectBean mySeleInfo;
    private String TAG = "AlipayBindingActivity";
    private String accountId;
    private String name;

    @Override
    protected int setStatusBarColor() {
        return 0;
    }

    @Override
    protected boolean isSettingStatusBarText() {
        return false;
    }

    @Override
    protected int loadLayout() {
        return R.layout.activity_alipay;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        //透明状态栏
        StatusBarUtils.Full(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        ButterKnife.bind(this);
        mySeleInfo = MySelfInfoBean.ObjectBean.CREATOR.createFromParcel(ParcelableUtils.unmarshall(
                SPUtils.getEditoString(SPUtils.SP_MYSELFINFO)));
        accountId = getIntent().getStringExtra("accountId");
        name = getIntent().getStringExtra("name");
        initView();
    }

    /**
     * @return
     * @method
     * @description 数据控件实例化
     * @date: 2018/11/19 10:36
     * @author: ljp
     */
    private void initView() {
        tvAlipayName.setText(name);
        tvAlipayName.setEnabled(false);
        tvAlipayAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String trim = s.toString().trim();
                if (trim.length() != 0) {
                    isAccount = true;
                } else {
                    isAccount = false;
                }
                if (isAccount) {
                    tvSure.setEnabled(true);
                    tvSure.setBackgroundResource(R.drawable.login_btn);
                } else {
                    tvSure.setEnabled(false);
                    tvSure.setBackgroundResource(R.drawable.login_btn_2);
                }
            }
            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        /*tvAlipayName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String trim = s.toString().trim();
                if (trim.length() != 0) {
                    isName = true;
                } else {
                    isName = false;
                }
                if (isName && isAccount) {
                    tvSure.setEnabled(true);
                    tvSure.setBackgroundResource(R.drawable.login_btn);
                } else {
                    tvSure.setEnabled(false);
                    tvSure.setBackgroundResource(R.drawable.login_btn_2);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });*/
    }

    /**
     * @return
     * @method
     * @description 提交账号
     * @date: 2018/11/19 10:44
     * @author: ljp
     */
    private void setSumit() {
        String name = tvAlipayName.getText().toString().trim();
        String account = tvAlipayAccount.getText().toString().trim();
        MeInfoService.submitAccount(accountId,SPUtils.getEditoString(SPUtils.SP_TOKEN), PhoneUtils.getDeviceInfo(context),
                account,name).subscribe(result -> {
            Log.i(TAG, "setSumit: ------"+result.toString());
                    if(result.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS){
                        toast_MakeText("提交成功");
                        startActivity(new Intent(activity,AccountSafelyActivity.class));
                        finish();
                    }else if(result.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                        Utils.setToken(context);
                    }else {
                        toast_MakeText(result.getMsg());
                    }
        },throwable -> {
                    toast_MakeText(getResources().getString(R.string.net_error));
            Log.i("throwable", "setSumit: ------"+throwable.toString());

        });


    }

    @OnClick({R.id.linear_back, R.id.tv_sure})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                finish();
                break;
            case R.id.tv_sure:
                setSumit();
                break;
        }
    }
}
